from django.shortcuts import render
import json

from django import http
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.forms.models import model_to_dict
from django.shortcuts import render, redirect
from django.views.generic import FormView
from django.views.generic.base import View
from django.views.generic import ListView
from django.db.models import Q

from rec.forms import UploadForm, ResultForm, HotForm, OutcomeForm
from utils.response import BadRequestJsonResponse, MethodNotAllowedJsonResponse, UnauthorizedJsonResponse, \
    ServerErrorJsonResponse, NotFoundJsonResponse
from rec import serializers
from rec.models import Profile




class Upload(FormView):
    """ 用户注册接口 """
    form_class = UploadForm
    http_method_names = ['post']
    print(http_method_names)

    def form_valid(self, form):
        """ 表单已经通过验证 """
        secret = self.kwargs.get('pk', None)
        if secret == 'erhke54dfs534':
            result = form.do_update(request=self.request)
            if result is not None:
                profile = result
                data = {
                    'profile': serializers.UserProfileSerializer(profile).to_dict()
                }
                return http.JsonResponse(data, status=201)
            return ServerErrorJsonResponse()
        return ServerErrorJsonResponse()

    def form_invalid(self, form):
        """ 表单没有通过验证 """
        err_list = json.loads(form.errors.as_json())
        return BadRequestJsonResponse(err_list)


class Result(FormView):
    form_class = ResultForm
    http_method_names = ['post']
    print(http_method_names)
    global item_cf_list
    item_cf_list = {}
    print(item_cf_list)

    def form_valid(self, form):
        """ 表单已经通过验证 """
        secret = self.kwargs.get('pk', None)
        if secret == 'dsfs5646df5a':
            result = form.do_result(request=self.request)
            if result is not None:
                profile = result
                data = {
                    'profile': serializers.UserResultSerializer(profile).to_dict(item_cf_list)
                }
                return http.JsonResponse(data, status=201)
            return ServerErrorJsonResponse()
        return ServerErrorJsonResponse()

    def form_invalid(self, form):
        """ 表单没有通过验证 """
        err_list = json.loads(form.errors.as_json())
        return BadRequestJsonResponse(err_list)

class Hot(FormView):
    form_class = HotForm
    http_method_names = ['post']
    print(http_method_names)

    def form_valid(self, form):
        """ 表单已经通过验证 """
        secret = self.kwargs.get('pk', None)
        print(secret)
        if secret == 'fjsjsdlfk3kdjf2':
            result = form.do_hot(request=self.request)
            if result is not None:
                profile = result
                data = {
                    'profile': serializers.UserHotSerializer(profile).to_dict(item_cf_list)
                }
                return http.JsonResponse(data, status=201)
            return ServerErrorJsonResponse()
        return ServerErrorJsonResponse()

    def form_invalid(self, form):
        """ 表单没有通过验证 """
        err_list = json.loads(form.errors.as_json())
        return BadRequestJsonResponse(err_list)

class Outcome(FormView):
    """ 用户注册接口 """
    form_class = OutcomeForm
    http_method_names = ['post']
    print(http_method_names)

    def form_valid(self, form):
        """ 表单已经通过验证 """
        secret = self.kwargs.get('pk', None)
        print(secret)
        if secret == 'sdf1231sdfs':
            result = form.do_outcome(request=self.request)
            if result is not None:
                profile = result
                data = {
                    'profile': serializers.OutProfileSerializer(profile).to_dict()
                }
                return http.JsonResponse(data, status=201)
            return ServerErrorJsonResponse()
        return ServerErrorJsonResponse()

    def form_invalid(self, form):
        """ 表单没有通过验证 """
        err_list = json.loads(form.errors.as_json())
        return BadRequestJsonResponse(err_list)